package com.reader.cms.controller.sys;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.reader.cms.controller.BaseController;
import com.reader.cms.entity.sys.SysMenu;
import com.reader.cms.service.sys.ISysMenuService;
import com.reader.core.entity.ResultMessageInfo;
import com.reader.core.exception.CustomException;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiahaikun
 * @since 2018-06-29
 */
@RestController
@RequestMapping("/api/SysMenu")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService iSysMenuService;

    /**
     * 菜单管理数据集
     * @return
     */
    @GetMapping("tree")
    @ResponseBody
    public ResultMessageInfo getMenus(){
        List<SysMenu> allMenus = iSysMenuService.getAllMenusManagement();
        return ResultMessageInfo.ofSuccess("查询成功！",allMenus);
    }

    /**
     * 菜单管理数据集
     * @return
     */
    @GetMapping("resources")
    @ResponseBody
    public ResultMessageInfo getAllResources(){
        List<SysMenu> resources = iSysMenuService.getAllResources();
        return ResultMessageInfo.ofSuccess("查询成功！",resources);
    }

    /**
     * 获取菜单对应按钮或资源
     * @param currentPage
     * @param size
     * @param parentId 父级ID
     * @param name  资源名称
     * @return
     */
    @GetMapping("button")
    @ResponseBody
    public ResultMessageInfo getMenuBtn(@RequestParam(value = "currentPage") int currentPage ,
                                                 @RequestParam(value = "size") int size,
                                                 @RequestParam(value = "parentId")Integer parentId,
                                                 @RequestParam(value = "name",required = false)String name){
        Page<Map<String, Object>> list = iSysMenuService.selectListPage(currentPage,size,parentId,name);
        return ResultMessageInfo.ofSuccess("查询成功！",list);
    }

    /**
     * 获取角色对应菜单
     * @param id 角色ID
     * @return
     */
    @GetMapping("menuOfRole")
    @ResponseBody
    public ResultMessageInfo getUserMenu(@RequestParam(value = "id")Integer id){
        List<Integer> menuIds = iSysMenuService.getRoleMenu(id);
        return ResultMessageInfo.ofSuccess("查询成功！",menuIds);
    }


    /**
     *  删除菜单
     * @param sysMenu
     * @return
     */
    @PostMapping("delete")
    @Transactional
    @ApiOperation(value="跟据ID列新记录", notes="id为必须项")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要删除对象ID", required = true, dataType = "int",paramType = "path")})
    public ResultMessageInfo deleteMenu(@RequestBody SysMenu sysMenu){
        ResultMessageInfo resultMessage=null;
        Integer userId = this.getUserId();
        sysMenu.setDelFlag(1); //逻辑删除
        sysMenu.setUpdtDt(new Date());
        sysMenu.setUpdtUserId(userId);
        boolean resultValue = iSysMenuService.updateById(sysMenu);
        if(!resultValue){
            return ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");
    }

    /**
        * @function 添加新记录
         * @param sysMenu 实体对象
         * @return
         */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody  SysMenu sysMenu) {
        Integer userId = this.getUserId();
        sysMenu.setInsertUserId(userId);
        sysMenu.setInsertDt(new Date());
        sysMenu.setUpdtUserId(userId);
        sysMenu.setDelFlag(0);
        ResultMessageInfo resultMessage=null;
        boolean resultValue =  iSysMenuService.insert(sysMenu);
        if(!resultValue){
            return ResultMessageInfo.ofMessage("添加失败！");
        }
        return ResultMessageInfo.ofSuccess("添加成功！");
    }

    /**
     * @function 修改
     * @param sysMenu 实体对象
     * @return
     */
    @PostMapping("update")
    @Transactional
    @ApiOperation(value="跟据ID列新记录", notes="id为必须项")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要更新对象ID", required = true, dataType = "int",paramType = "path")})
    public ResultMessageInfo update(@RequestBody  SysMenu sysMenu) {
        Integer userId = this.getUserId();
        sysMenu.setUpdtDt(new Date());
        sysMenu.setUpdtUserId(userId);
        ResultMessageInfo resultMessage=null;
        boolean resultValue =  iSysMenuService.updateById(sysMenu);
        if(!resultValue){
            return ResultMessageInfo.ofMessage("修改失败！");
        }
        return ResultMessageInfo.ofSuccess("修改成功！");
    }

    /**
     * @function 批量删除对象
     * @param data 键为ids值为整型数组的json格式
     * @return
     */
    @PostMapping("deleteIds")
    @Transactional
    @ApiOperation(value="跟据ID删除记录", notes="键为ids值为整型数组的json格式")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "id列表", required = true, dataType = "int",paramType = "path")})
    public ResultMessageInfo deleteBatchIds(@RequestBody String data){
        ResultMessageInfo resultMessage=null;
        List<Long> ids = JSON.parseArray( JSON.parseObject(data).getString("ids"),Long.class);
        boolean resultValue =  iSysMenuService.deleteBatchIds(ids);
        if(resultValue){
            resultMessage = ResultMessageInfo.ofSuccess("删除成功!",resultValue);
        }
        else{
            return ResultMessageInfo.ofMessage("删除失败!");
        }
        return resultMessage;

    }

}

